package routing;

import geocoding.CustomNodes;

import java.util.ArrayList;

import android.location.Location;
import android.util.Log;

public class SegmentMaker {
	
	String TAG = "URN"; 
	
public ArrayList<Segment> makeSegments(CustomNodes cn, Trip myTrip) {
		
//		Log.d(TAG, "makeSegments start");
	
		
		ArrayList<Segment> segments = new ArrayList<Segment>();
		ArrayList<Integer>segmentNodes = new ArrayList<Integer>();
		
		int count = 0; 
		
		
		for (int i=0; i < myTrip.getTripList().size(); i++){
			
			
//			Log.d(TAG, "checking " + cn.getOsmID(myTrip.getTripItem(i)));
			
			if (i==0) {

				segmentNodes.add(myTrip.getTripItem(i)); 	
			}
			
			else if (i==myTrip.getTripList().size()-2) {

				segmentNodes.add(myTrip.getTripItem(i)); 	
			}
			
			else if (i==myTrip.getTripList().size()-1) {

				segmentNodes.add(myTrip.getTripItem(i)); 
				Segment segment = new Segment();
				segment.setId(count); 
				
				for (int n : segmentNodes) {
					
					Node node = new Node();
					node.setId(n); 
					segment.addNode(node); 
					
				}

				
//				Log.d(TAG, "in SM segment " + segment.getId() + " nodes: " + segment.getNodes().toString());
				segmentNodes.clear(); 
				count++; 
				segments.add(segment);
//				Log.d(TAG, "in SM segment " + segment.getId() + " nodes: " );
				for (int x=0; x<segment.getNodes().size(); x++) {
					Log.d(TAG, "   " + segment.getNodes().get(x));
				}	
				
				break; 
			}
			
			else if ((getAngle(myTrip.getTripItem(i), myTrip.getTripItem(i+1), myTrip.getTripItem(i+2),cn) < 25)  &&  
					(getAngle(myTrip.getTripItem(i), myTrip.getTripItem(i+1), myTrip.getTripItem(i+2),cn) > -25))  
			{

				segmentNodes.add(myTrip.getTripItem(i)); 		
			}
			
			else {

				segmentNodes.add(myTrip.getTripItem(i)); 
				Segment segment = new Segment();
				segment.setId(count); 
				for (int n : segmentNodes) {
					
					Node node = new Node();
					node.setId(n);
					segment.addNode(node); 
					
				}
				segments.add(segment);
				segmentNodes.clear(); 
				count++;
//				Log.d(TAG, "in SM segment " + segment.getId() + " nodes: " + segment.getNodes().toString());
			}
			
		}
		
//		Log.d(TAG, " segments SM ende: " + segments.get(0).getNodes().size());
		return segments; 
	}
	
	public float getAngle(int firstItem, int secondItem, int thirdItem, CustomNodes cn){
		
		Location firstLoc = new Location("x");
		Location secondLoc = new Location("x");
		Location thirdLoc = new Location("x");
		
		firstLoc.setLatitude(cn.getDlati(firstItem));
		firstLoc.setLongitude(cn.getDlongi(firstItem));
		
		secondLoc.setLatitude(cn.getDlati(secondItem));
		secondLoc.setLongitude(cn.getDlongi(secondItem));
		
		thirdLoc.setLatitude(cn.getDlati(thirdItem));
		thirdLoc.setLongitude(cn.getDlongi(thirdItem));

		float angle; 
		float bearing1;
		float bearing2;
		

		bearing1 = firstLoc.bearingTo(secondLoc);

		bearing2 = secondLoc.bearingTo(thirdLoc);

		angle = bearing1 - bearing2;
		
		return angle;
		
	}
	
	public void logsegment(Segment s, CustomNodes cn) {
		
//		Log.d(TAG, "Made segment, ID: " + s.getId());
		ArrayList<Node>nodes = s.getNodes(); 
		for (int i=0; i<nodes.size(); i++) {
			
			String osmid = cn.getOsmID(nodes.get(i).getId()); 
//			Log.d(TAG, "    node: " + osmid);
			
		}
		
	}

}
